# 使用的场景：数据采集的时候，需要绕过登陆  然后进入到某个页面
# 个人信息页面是utf-8  但是还报错了编码错误  因为并没有进入到个人信息页面 而是跳转到了登录页面
# 那么登陆页面不是utf-8  所以报错

# 什么情况下访问不成功
# 因为请求头的信息不够 所以访问不成功

import urllib.request

# https://weibo.com/7463499374/profile?topnav=1&wvr=6&is_all=1


url='https://weibo.com/7463499374/profile?topnav=1&wvr=6&is_all=1'

headers={
# :authority: weibo.com
# :method: GET
# :path: /7463499374/profile?topnav=1&wvr=6&is_all=1
# :scheme: https
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
'cache-control': 'max-age=0',

    # cookie 中携带着你的登陆信息 如果有登陆之后的cookie 那么我们就可以携带着cookie进入到任何页面
'cookie': '_s_tentry=passport.weibo.com; Apache=161254275429.11462.1661262142914; SINAGLOBAL=161254275429.11462.1661262142914; ULV=1661262142918:1:1:1:161254275429.11462.1661262142914:; WBtopGlobal_register_version=2022082321; XSRF-TOKEN=Fl0Ek6yQXUOG7x2D_Uv7qD2A; WBPSESS=IawIaCISeX-46VmeRocrJyYA6Ya6cHdvxd9li5Km9bGLJGzvhWfUq0C6bRtPigurVT-XUQpeOEMGu7_6KworkDjlblVrSCpYzHDcgqeJP2eskKZtGxZkaXZBnXb6HWKvAvbQ4frRfc80cqLpkUDjS-8bMBKvgkmOcLdulw-mP7I=; login_sid_t=4c58a0d00ba25b534836fab3eaf483e4; cross_origin_proto=SSL; wb_view_log=1536*8641.25; PC_TOKEN=43780691bb; appkey=; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9Wh2ryOZVU06yScy0Mf_3ZRS5JpX5o275NHD95QNShq0Sh.4e0MXWs4DqcjiCcLV9s2Eeh2p; SSOLoginState=1663061255; SUB=_2A25OJD1YDeRhGeFK7VEV-SfPzDiIHXVtUCmQrDV8PUNbmtAKLUOgkW9NQwVUsV8P3atzhOHqsZh-vRR9OcmaGPEO; ALF=1663666060; wb_view_log_7463499374=1536*8641.25; webim_unReadCount=%7B%22time%22%3A1663062344540%2C%22dm_pub_total%22%3A0%2C%22chat_group_client%22%3A0%2C%22chat_group_notice%22%3A0%2C%22allcountNum%22%3A1%2C%22msgbox%22%3A0%7D',
# referer  判断当前路径是不由上一个路径进来的  一般情况下是做图片的防盗链
'referer': 'https://weibo.com/signup/signup.php',
'sec-ch-ua': '"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'document',
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'same-origin',
'sec-fetch-user': '?1',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}

# 请求对象的定制
request = urllib.request.Request(url=url,headers=headers)

# 模拟浏览器向服务器发送请求
response = urllib.request.urlopen(request)
# 获取响应的数据
content = response.read().decode('gb2312')

print(content)

# 将数据保存到本地
with open('weibo.html','w',encoding='gb2312') as fp:
    fp.write(content)




